package com.password.safe.controller;

import javax.swing.JMenu;
import javax.swing.JTextArea;

import org.apache.log4j.Logger;

import com.password.safe.service.PasswordSafeService;
import com.password.safe.util.UserCache;

public class PasswordSafeController extends BaseController {
	private static Logger logger = Logger.getLogger(PasswordSafeController.class);
	private PasswordSafeService passwordSafeService;
	
	private JTextArea editArea;
	private JMenu saveMenu;
	
	public void savePasswords() {
		logger.debug("save the passwords begin...");
		String username = UserCache.getUsername();
		String salt = UserCache.getPassword();
		String addText = username + ":" + salt + "\n";
		passwordSafeService.saveText(salt, addText + this.editArea.getText());
		logger.debug("save the passwords end...");
	}
	
	public String getPasswords() {
		String username = UserCache.getUsername();
		String salt = UserCache.getPassword();
		String text = passwordSafeService.getText(salt);
		String replacedText = username + ":" + salt + "\n";
		return text.replace(replacedText, "");
	}

	public JTextArea getEditArea() {
		return editArea;
	}
	
	public void setEditArea(JTextArea editArea) {
		this.editArea = editArea;
	}
	
	public JMenu getSaveMenu() {
		return saveMenu;
	}
	
	public void setSaveMenu(JMenu saveMenu) {
		this.saveMenu = saveMenu;
	}

	public PasswordSafeService getPasswordSafeService() {
		return passwordSafeService;
	}

	public void setPasswordSafeService(PasswordSafeService passwordSafeService) {
		this.passwordSafeService = passwordSafeService;
	}
	
}
